package br.com.fiap.mercadinho.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fiap.mercadinho.bean.Product;
import br.com.fiap.mercadinho.common.DAOException;
import br.com.fiap.mercadinho.common.RequestKey;
import br.com.fiap.mercadinho.dao.ProductDAO;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProductServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
			ProductDAO dao = new ProductDAO();

		try {
			List<Product> products = dao.findAllProducts();
			request.setAttribute("productList", products);
			sendResponse(request, response);

		} catch (DAOException e) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					e.getMessage());
			sendResponse(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ProductDAO dao = new ProductDAO();

		try {
			String action = request.getParameter("action");
			request.setAttribute("action", action);
			// refresh products list
			request.setAttribute("productList", dao.findAllProducts());

			if (action.equals("show")) {
				List<Product> products = dao.findAllProducts();
				request.setAttribute("productList", products);
			} else if (action.equals("add")) {
				Product prod = getProductFromRequest(request);
				if (!isValidProduct(request, prod)) {
					sendResponse(request, response);
					return;
				} else {
					prod.saveProductAndRegisterInStock();
					List<Product> products = dao.findAllProducts();
					request.setAttribute("productList", products);
				}
			} else if (action.equals("select")) {
				Integer idProduct = Integer.parseInt(request
						.getParameter("productIdSelected"));
				Product product = dao.findProductById(idProduct);
				request.setAttribute("product", product);
				List<Product> products = dao.findAllProducts();
				request.setAttribute("productList", products);
			} else if (action.equals("update")) {
				Product prod = getProductFromRequest(request);
				if (!isValidProduct(request, prod)) {
					sendResponse(request, response);
					return;
				} else {
					dao.updateProduct(prod);
					List<Product> products = dao.findAllProducts();
					request.setAttribute("productList", products);
					request.setAttribute("action", "show");

				}
			}
			sendResponse(request, response);
		} catch (DAOException e) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					e.getMessage());
			sendResponse(request, response);
		}

	}

	private void sendResponse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("product.jsp").forward(request, response);
	}

	private Product getProductFromRequest(HttpServletRequest request) {
		Product product = new Product();
		product.setId(request.getParameter("productIdSelected"));
		product.setDescription(request.getParameter("description"));
		product.setPrice(request.getParameter("price"));

		return product;
	}

	private boolean isValidProduct(HttpServletRequest request, Product prod) {

		String validateResult = prod.validateProduct();

		if (!validateResult.equals("")) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					validateResult);
			request.setAttribute("product", getProductFromRequest(request));
			return false;
		}
		return true;

	}

}
